package com.example.media;

import com.example.media.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        // 申请权限
        requestPermissions();
    }

    private void requestPermissions() {
        // 需要处理的权限
        String[] permissions = {
                "ohos.permission.CAMERA",
                "ohos.permission.READ_MEDIA",
                "ohos.permission.WRITE_MEDIA",
                "ohos.permission.READ_USER_STORAGE",
                "ohos.permission.WRITE_USER_STORAGE"};
        // 可动态授权的权限
        List<String> permissionsToProcess = new ArrayList<>();
        // 遍历需要处理的权限
        for (String permission : permissions) {
            // 判断需要处理的权限是否可动态授权
            if (verifySelfPermission(permission) != 0
                    && canRequestPermission(permission)) {
                permissionsToProcess.add(permission);
            }
        }
        // 弹窗申请权限
        requestPermissionsFromUser(permissionsToProcess.toArray(new String[0]), 0);
    }

}
